document.ready(function() {
    openLoad();

    var param = {};
    param.ctrl = 'account';
    param.func = 'getRoleMenu';
    
    sendAjax(param, function(result) {
        menuList.list = result.menuList;

        setDisplay('mainBg', '');
        closeLoad();
    });
});

function setCode(id, flag) {
    if (flag == 'delete') {
        setValue('code', getValue('code').replace('●' + id + '●', ''));
    }
    else {
        setValue('code', getValue('code') + '●' + id + '●');
    }
}

function selectTopMenu(id) {
    if (getClass('menu_top_right_' + id) == 'role-top-right') {
        setClass('menu_top_left_' + id, 'role-top-left-select');
        setClass('menu_top_right_' + id, 'role-top-right-select');
        setCode(id, 'add');
    }
    else {
        setClass('menu_top_left_' + id, 'role-top-left');
        setClass('menu_top_right_' + id, 'role-top-right');
        setCode(id, 'delete');
    }
}

function selectParentMenu(id) {
    if (getClass('menu_parent_right_' + id) == 'role-parent-right') {
        setClass('menu_parent_left_' + id, 'role-parent-left-select');
        setClass('menu_parent_right_' + id, 'role-parent-right-select');
        setCode(id, 'add');
    }
    else {
        setClass('menu_parent_left_' + id, 'role-parent-left');
        setClass('menu_parent_right_' + id, 'role-parent-right');
        setCode(id, 'delete');
    }
}

function selectChildMenu(id) {
    if (getClass('menu_child_' + id) == 'role-child') {
        setClass('menu_child_' + id, 'role-child-select');
        setCode(id, 'add');
    }
    else {
        setClass('menu_child_' + id, 'role-child');
        setCode(id, 'delete');
    }
}

function save() {
    if (isEmpty('rolemc') || isWicked('rolemc')) {
        showTip('角色名称格式错误');
        return;
    }
    if (isEmpty('roleType')) {
        showTip('角色类型不能为空');
        return;
    }
    if (isEmpty('sx') || !isNum('sx')) {
        showTip('顺序格式错误');
        return;
    }
    if (isEmpty('code')) {
        showTip('菜单权限不能为空');
        return;
    }

    var param = {};
    param.ctrl = 'account';
    param.func = 'saveRole';
    param.flag = getAjaxValue('flag');
    param.roleid = getAjaxValue('roleid');
    param.rolemc = getAjaxValue('rolemc');
    param.roleType = getAjaxValue('roleType');
    param.code = getAjaxValue('code');
    param.sx = getAjaxValue('sx');
    param.status = getItemByName('status').checked ? '1' : '0';

    sendAjax(param, function(result) {
        toUrl('admin.php?m=account&c=rolelist');
    });
}